-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
GH-139174: Add pathlib.Path.info.stat()
#139279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Add `stat()` method to `pathlib.Path.info` that returns a (possibly cached) `os.stat_result` object. We don't add it to `pathlib.types.PathInfo` because it's too specific to local filesystem paths. This requires a bit of reworking of the docs to explain! Rename `pathlib._Info` to `pathlib.Info` and document it, including the new `stat()` method. Ensure it can't be instantiated by users. Move the existing docs for `pathlib.types` to a new page.
encukou
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
I appreciate that stat() isn't the best API, but it is useful for interop with existing systems.
| On Windows, if the path was generated by scanning a directory, then the | ||
| ``st_ino``, ``st_dev`` and ``st_nlink`` attributes of the | ||
| :class:`~os.stat_result` are always set to zero. Call :meth:`Path.stat` | ||
| to get these attributes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, would it be better to call stat() on access in this case?
The platform difference looks ugly at the API level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
os.DirEntry.stat() has the same shortcoming. Would it be any better if we recommended os.stat() instead of, or in addition to, Path.stat()?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we implement #140127, we'll be able to recommend Path.metadata(cached=False).stat()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even there it would be great if Path.metadata(cached=True).stat() returned a complete stat object.
IMO, os.DirEntry is an implementation detail that pathlib should hide.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, will do. This will require merging the _StatResultInfo and _DirEntryInfo classes together I think. I've opened a new PR to do that: #140155
Add
stat()method topathlib.Path.infothat returns a (possibly cached)os.stat_resultobject. We don't add it topathlib.types.PathInfobecause it's too specific to local filesystem paths. This requires a bit of reworking of the docs to explain!Rename
pathlib._Infotopathlib.Infoand document it, including the newstat()method. Ensure it can't be instantiated by users. Move the existing docs forpathlib.typesto a new page.pathlib.Path.info#139174📚 Documentation preview 📚: https://cpython-previews--139279.org.readthedocs.build/